import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json


class Testcrd:

    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/1_get_crd_list.yaml'))
    def test_get_crd_list(self, crdinfo, get_env_info):
        # 获取crd列表接口
        baseUrl = get_env_info
        url = baseUrl + crdinfo['request']['url']
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/2_create_crd.yaml'))
    def test_create_crd(self, crdinfo, get_env_info):
        # 创建crd接口
        baseUrl = get_env_info
        url = baseUrl + crdinfo['request']['url']
        method = crdinfo['request']['method']
        data = crdinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in crdinfo['name']:
            crdname=req.json()['metadata']['name']
            globals()['crdname']=crdname
            group=req.json()['spec']['group']
            globals()['group'] = group
            plural=req.json()['spec']['names']['plural']
            globals()['plural'] = plural
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/3_get_specific_crd.yaml'))
    def test_get_specific_crd(self, crdinfo, get_env_info):
        # 获取指定crd接口
        baseUrl = get_env_info
        url = baseUrl + crdinfo['request']['url'].replace('{crdname}',globals()['crdname'])
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in crdinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/4_update_crd.yaml'))
    def test_update_crd(self, crdinfo, get_env_info):
        # 更新指定crd接口
        baseUrl = get_env_info
        url = baseUrl + crdinfo['request']['url'].replace('{crdname}',globals()['crdname'])
        method = crdinfo['request']['method']
        crdinfo['request']['data'] = globals()['updatereqbody']
        data = crdinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/5_update_section_crd.yaml'))
    def test_update_section_crd(self, crdinfo, get_env_info):
        # 部分更新指定crd的接口
        baseUrl = get_env_info
        url = baseUrl + crdinfo['request']['url'].replace('{crdname}',globals()['crdname'])
        method = crdinfo['request']['method']
        data = crdinfo['request']['data']
        header = crdinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/6_get_cr_list.yaml'))
    def test_get_cr_list(self, crinfo, get_env_info):
        # 获取cr列表接口
        baseUrl = get_env_info
        url = baseUrl + crinfo['request']['url'].replace('{group}',globals()['group']).replace('{plural}',globals()['plural'])
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crinfo',YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/7_create_cr.yaml'))
    def test_create_cr(self, crinfo, get_env_info):
        # 创建cr接口
        baseUrl = get_env_info
        url = baseUrl + crinfo['request']['url'].replace('{group}',globals()['group']).replace('{plural}',globals()['plural']).replace('{namespace}','default')
        method = crinfo['request']['method']
        data = crinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        if '不正确' not in crinfo['name']:
            crname = req.json()['metadata']['name']
            globals()['crname'] = crname
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/8_get_specific_cr.yaml'))
    def test_get_specific_cr(self, crinfo, get_env_info):
        # 获取指定cr接口
        baseUrl = get_env_info
        url = baseUrl + crinfo['request']['url'].replace('{group}',globals()['group']).replace('{plural}',globals()['plural']).replace('{namespace}','default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in crinfo['name']:
            updatereqbody = req.json()
            globals()['updatereqbody'] = updatereqbody
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/9_update_cr.yaml'))
    def test_update_cr(self, crinfo, get_env_info):
        # 更新指定cr接口
        baseUrl = get_env_info
        url = baseUrl + crinfo['request']['url'].replace('{group}',globals()['group']).replace('{plural}',globals()['plural']).replace('{namespace}','default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        crinfo['request']['data'] = globals()['updatereqbody']
        data = crinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/10_update_section_cr.yaml'))
    def test_update_section_cr(self, crinfo, get_env_info):
        # 部分更新指定cr的接口
        baseUrl = get_env_info
        url = baseUrl + crinfo['request']['url'].replace('{group}',globals()['group']).replace('{plural}',globals()['plural']).replace('{namespace}','default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        data = crinfo['request']['data']
        header = crinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/11_delete_specific_cr.yaml'))
    def test_delete_specific_cr(self, crinfo, get_env_info):
        # 删除cr接口
        baseUrl = get_env_info
        url = baseUrl + crinfo['request']['url'].replace('{group}',globals()['group']).replace('{plural}',globals()['plural']).replace('{namespace}','default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/crd_and_cr/12_delete_specific_crd.yaml'))
    def test_delete_specific_crd(self, crdinfo, get_env_info):
        # 删除crd接口
        baseUrl = get_env_info
        url = baseUrl + crdinfo['request']['url'].replace('{crdname}',globals()['crdname'])
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']